//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.starinside.wp.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.UUID;
import java.util.stream.Collectors;

import com.starinside.wp.foundation.exception.BusinessException;
import com.starinside.wp.foundation.model.StatusResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.ResourceUtils;

public class RemoteCallUtil {
    private static final String SIGN_BASE_VALUE = "fishstarinside";
    public static String PLATFORM_NAME = "commons";

    public RemoteCallUtil() {
    }

    private static String md5(String plainText) {
        Object var1 = null;

        byte[] secretBytes;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes());
            secretBytes = md.digest();
        } catch (NoSuchAlgorithmException var4) {
            throw new RuntimeException("没有md5这个算法！");
        }

        String md5code = (new BigInteger(1, secretBytes)).toString(16);

        for(int i = 0; i < 32 - md5code.length(); ++i) {
            md5code = "0" + md5code;
        }

        return md5code;
    }

    public static String authSigh(String urlStr, String compver, String reqUuid, String platform) {
        String path = "";

        try {
            URL url = new URL(urlStr);
            path = url.getPath();
        } catch (MalformedURLException var6) {
            var6.printStackTrace();
        }

        String signBase = path + compver + reqUuid + platform + "fishstarinside";
        return md5(signBase);
    }

    public static void signRequest(Map headers, String url, String ticket) {
        if (StringUtils.isNotBlank(ticket)) {
            headers.put("ticket", ticket);
        } else {
            String uuid = UUID.randomUUID().toString();
            headers.put("uuid", uuid);
            headers.put("compver", "1");
            headers.put("platform", PLATFORM_NAME);
            headers.put("auth", authSigh(url, "1", uuid, PLATFORM_NAME));
        }

    }

    public static <T> List<T> callOfList(String url, Map params, String ticket, Class<T> clazz, String errorMsg) {
        try {
            Map headers = new HashMap();
            signRequest(headers, url, ticket);
            String responseJson = HttpClientUtil.sendPost(url, params, headers);
            StatusResponse response = JsonUtil.getResponse(responseJson, errorMsg);
            if (response.getData() instanceof List) {
                if (Objects.nonNull(clazz) && !clazz.equals(Map.class) && !Map.class.isAssignableFrom(clazz) && !MapConvertUtil.isNormalClass(clazz)) {
                    List<Map<String, Object>> list = (List)response.getData();
                    return (List)list.stream().map((o) -> {
                        return MapConvertUtil.mapToObject(o, clazz);
                    }).collect(Collectors.toList());
                } else {
                    return (List)response.getData();
                }
            } else {
                return null;
            }
        } catch (BusinessException var9) {
            var9.printStackTrace();
            throw var9;
        } catch (Exception var10) {
            var10.printStackTrace();
            throw new BusinessException("999998", errorMsg);
        }
    }
    public static <T> T call(String url, Map params, String ticket, Class<T> clazz, String errorMsg) {
        try {
            Map headers = new HashMap();
            signRequest(headers, url, ticket);
            String responseJson = HttpClientUtil.sendPost(url, params, headers);
            StatusResponse response = JsonUtil.getResponse(responseJson, errorMsg);
            if (response.getData() instanceof Map) {
                if (Objects.nonNull(clazz) && !clazz.equals(Map.class) && !Map.class.isAssignableFrom(clazz)) {
                    return MapConvertUtil.mapToObject((Map<String, Object>) response.getData(), clazz);
                }
            } else if (Objects.isNull(response.getData())) {
                return null;
            }

            return (T) response.getData();
        } catch (BusinessException var8) {
            throw new BusinessException(var8.getErrorCode(), var8.getMessage());
        } catch (Exception var9) {
            throw new BusinessException("999998", errorMsg);
        }
    }

    static {
        PLATFORM_NAME = "wp";
    }
}
